package com.c.ctgproject.utils

import android.content.Context
import android.graphics.*
import android.util.TypedValue
import androidx.core.content.ContextCompat
import com.c.ctgproject.R
import java.io.BufferedOutputStream
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.text.SimpleDateFormat
import java.util.*

object WaterRemark {

    /**1:左上角 2:底部中间  3:右上角 4:左下角 5:右下角 6:正中间*/
    fun handlerWaterRemark2File(context: Context, file: File, fileName: String): File {
        val bitmap = BitmapFactory.decodeFile(file.absolutePath)
        //原始图片宽高
        val width: Int = bitmap.width
        val height: Int = bitmap.height
        //创建一个跟原图一样大的Bitmap
        val newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) // 创建一个新的和SRC长度宽度一样的位图
        //将该图片作为画布
        val canvas = Canvas(newBitmap)
        //在画布 0，0坐标上开始绘制原始图片
        canvas.drawBitmap(bitmap, 0f, 0f, null)
        //设置画笔颜色
        val paint = Paint()
        paint.color = Color.WHITE
        //底色
        //画笔
        val maskingPaint = Paint()
        maskingPaint.color = ContextCompat.getColor(context, R.color.black70)
        //设置文字大小
        val textSize = sp2px(context, width/80).toFloat()
        paint.textSize = textSize
        //文字的Margin
        val margin = dp2px(context, width/80f)/2f
        val msg = "庖丁解车"
        //文字总宽度
        val textWidth = msg.length * textSize
        //时间
        val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA)
        val time = format.format(Date())

        //时间宽度
        val timeWidth = time.length * textSize/2

        //绘制时间
        canvas.drawRect(0f, 0f, timeWidth + margin * 3, textSize + margin , maskingPaint)
        canvas.drawText(time, margin, textSize/2 + margin, paint)

        //绘制文字
        val x = (width / 2f) - (textWidth / 2)
        val y = height.toFloat() - margin
        //底色
        canvas.drawRect(x - margin, y - margin * 3, x + textWidth + margin, height.toFloat(), maskingPaint)
        canvas.drawText(msg, x, y, paint)

        //保存图片
        canvas.save()
        canvas.restore()
        return saveBitmapFile(context, newBitmap, fileName)
    }


    private fun saveBitmapFile(context: Context, bitmap: Bitmap, name: String): File {
        val file = File(context.getExternalFilesDir(null)?.absolutePath + name + ".jpg") //将要保存图片的路径
        try {
            val bos = BufferedOutputStream(FileOutputStream(file))
            //100意味着不压缩
            bitmap.compress(Bitmap.CompressFormat.JPEG, 30, bos)
            bos.flush()
            bos.close()
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return file
    }


    private fun dp2px(context: Context, dpValue: Float): Int {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, context.resources.displayMetrics).toInt()
    }

    private fun sp2px(context: Context, spValue: Int): Int {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spValue.toFloat(), context.resources.displayMetrics).toInt()
    }
}